package org.bug2222.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.bug2222.reggie.common.CustomException;
import org.bug2222.reggie.dto.DishDto;
import org.bug2222.reggie.entity.Dish;
import org.bug2222.reggie.entity.DishFlavor;
import org.bug2222.reggie.entity.SetmealDish;
import org.bug2222.reggie.mapper.DishMapper;
import org.bug2222.reggie.service.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Transactional
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {
    @Autowired
    private DishFlavorService dishFlavorService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private SetmealDishService setmealDishService;
    @Autowired
    private SetmealService setmealService;
    /**
     * 新增菜品，同时保存对应的口味数据
     * @param dishDto
     */
    @Override
    @Transactional
    public void saveWithFlavor(DishDto dishDto) {
        //保存菜品的基本信息到dish
        save(dishDto);

        Long dishId = dishDto.getId();

        //菜品口味
        List<DishFlavor> flavors = dishDto.getFlavors();

        for (DishFlavor dishFlavor:flavors){
            dishFlavor.setDishId(dishId);
        }

        //保存菜品口味数据到dish_flavor
        dishFlavorService.saveBatch(flavors);
    }

    /**
     * 根据ID查询菜品信息和对应的口味
     * @param id
     * @return
     */
    @Override
    public DishDto getByIdWithFlavor(Long id) {
        //查询菜品基本信息，从dish表查询
        Dish dish = getById(id);

        DishDto dishDto = new DishDto();
        BeanUtils.copyProperties(dish,dishDto);

        //查询当前菜品对应的口味，从dish_flavor表查询
        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DishFlavor::getDishId,dish.getId());
        List<DishFlavor> flavors = dishFlavorService.list(queryWrapper);

        dishDto.setFlavors(flavors);
        return dishDto;
    }


    @Override
    @Transactional
    public void updateWithFlavor(DishDto dishDto) {
        //更新dish表基本信息
        updateById(dishDto);
        //清理当前菜品对应的口味数据,dish_flavor表的delete操作
        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DishFlavor::getDishId,dishDto.getId());
        dishFlavorService.remove(queryWrapper);
        //添加当前提交的口味数据,dish_flavor表的insert操作
        List<DishFlavor> flavors = dishDto.getFlavors();
        for (DishFlavor dishFlavor:flavors){
            dishFlavor.setDishId(dishDto.getId());
        }

        dishFlavorService.saveBatch(flavors);

    }

    /**
     * 根据id集合批量修改菜品售卖状态
     * @param ids
     * @param status
     */
    @Override
    @Transactional
    public void updateWithStatus(List<Long> ids, int status) {
        LambdaUpdateWrapper<Dish> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.in(Dish::getId,ids);
        updateWrapper.set(Dish::getStatus,status);
        //根据传过来的id集合来修改状态
        update(updateWrapper);
        if (status==0) {
            LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.in(SetmealDish::getDishId, ids);
            //根据传过来的ID集合获取setmealdish集合
            List<SetmealDish> setmealDishes = setmealDishService.list(queryWrapper);

            List<Long> setmealIds = new ArrayList<>();
            setmealDishes.forEach(item -> {
                //提取setmealdish集合中的setmealid
                setmealIds.add(item.getSetmealId());
            });
            //如果菜品停售，将菜品关联的套餐也停售
            //根据提取出来的setmealid集合，调用service中的停售方法
            setmealService.updateWithStatus(setmealIds, status);
        }
    }

    @Override
    public void remove(List<Long> ids) {

        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(Dish::getId,ids);
        List<Dish> list = list(queryWrapper);


        list.forEach(item->{
            //如果是在售状态不能删，抛出异常
            if (item.getStatus()==1) throw new CustomException("在售状态不能删除");

            //如果有套餐包括不能删，抛出异常
            LambdaQueryWrapper<SetmealDish> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(SetmealDish::getDishId,item.getId());
            if (setmealDishService.list(lambdaQueryWrapper).size()>0) {
                throw new CustomException("包含在某些套餐中，不能删除");
            }
        });

        remove(queryWrapper);


    }

}
